﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using CBS.Common;
using CBS.DB;
using CBS.DB.CBSEntity;
using CBS.Model.ManageModel;
using CBS.Model.UIModel;
using CBS.Model.ViewModel;

namespace CBS.APP
{
    public class SemesterApp
    {
        public static bool CreateSemester(SemesterViewModel newSemester)
        {
            using CBSContext cbs = new();
            cbs.cbs_semester.Add(new cbs_semester
            {
                SemesterName = newSemester.SemesterName,
                Memo = newSemester.Memo,
                StartTime = newSemester.StartTime,
                EndTime = newSemester.EndTime,
            });

            return cbs.SaveChanges() > 0;
        }

        public static bool UpdateSemester(SemesterViewModel semester)
        {
            using CBSContext cbs = new();
            var entity = cbs.cbs_semester.First(t=>t.SemesterId == semester.SemesterId);
            entity.SemesterName = semester.SemesterName;
            entity.Memo = semester.Memo;    
            entity.StartTime = semester.StartTime;
            entity.EndTime = semester.EndTime;

            return cbs.SaveChanges() > 0;
        }

        public static List<SemesterViewModel> GetList(Pagination page, string? keyword)
        {
            if (string.IsNullOrWhiteSpace(keyword))
            {
                return GetList(null, page);
            }
            else
            {
                return GetList(t => t.SemesterName.Contains(keyword), page);
            }
            
        }

        public static List<SemesterViewModel> GetList(Expression<Func<cbs_semester, bool>>? predicate, Pagination page)
        {
            List<cbs_semester> list;
            RepositoryBase<cbs_semester> repo = new();
            if (predicate == null)
            {
                list = repo.FindList(page);
            }
            else
            {
                list = repo.FindList(predicate, page);
            }
            

            return PropertyHelper.Assignment<SemesterViewModel, cbs_semester>(list);
        }

        public static List<SemesterSelectModel> GetSelectList() 
        {
            using CBSContext cbs = new();
            var list = (from s in cbs.cbs_semester
                        orderby s.SemesterId descending
                        select new SemesterSelectModel
                       {
                           SemesterId = s.SemesterId,
                           SemesterName = s.SemesterName
                       }).ToList();

            return list;
        }

        public static bool DeleteSemester(int SemesterId, out string msg)
        {
            msg = "";
            using CBSContext cbs = new();

            if (cbs.cbs_course_plan.Any(t=>t.SemesterId == SemesterId))
            {
                msg = "数据已经被使用，不能删除。";
                return false;
            }

            cbs.cbs_semester.Remove(cbs.cbs_semester.First(t => t.SemesterId == SemesterId));

            return cbs.SaveChanges() > 0;
        }

        public static bool BatchDeleteSemester(string key, out string msg)
        {
            msg = "";
            using CBSContext cbs = new();

            var semesterList = cbs.cbs_semester.Where(t => t.SemesterName.Contains(key)).ToList();
            var ids = semesterList.Select(t => t.SemesterId).ToList();
            if (cbs.cbs_course_plan.Any(t=> ids.Contains(t.SemesterId)))
            {
                msg = "未找到将要删除的数据，请重试。";
                return false;
            }

            cbs.cbs_semester.RemoveRange(semesterList);
            return cbs.SaveChanges() > 0;
        }

        public static SemesterViewModel GetForm(int SemesterId)
        {
            using CBSContext cbs = new();
            var entity = cbs.cbs_semester.FirstOrDefault(t => t.SemesterId == SemesterId);

            return PropertyHelper.Assignment<SemesterViewModel, cbs_semester>(entity);
        }
    }
}
